//
//  main.cpp
//  123
//
//  Created by stflaptop on 5/28/13.
//  Copyright (c) 2013 stflaptop. All rights reserved.
//

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stdexcept>

class Stack
{
public:
    Stack(int capacity_)
    : elements(NULL)
    , top(0)
    , capacity(capacity_)
    {
        if (capacity <= 0)
        {
            throw std::invalid_argument("capacity should be larger than 0");
        }
        elements = new int[capacity];
    }
    ~Stack()
    {
        delete[] elements;
    }
    bool isFull()
    {
        return (top >= capacity);
    }
    void push(int val)
    {
        if (isFull())
        {
            throw std::overflow_error("stack is full");
        }
        elements[top] = val;
        (top)++;
    }
    bool isEmpty()
    {
        return (top == 0);
    }
    int pop()
    {
        if (isEmpty())
        {
            throw std::underflow_error("stack is empty");
        }
        (top)--;
        return (elements[top]);
    }
    int size()
    {
        return (top);
    }
    int getCapacity()
    {
        return (capacity);
    }
private:
    int* elements;
    int top;
    int capacity;
};

int main()
{
    Stack st(10);
    try
    {
        for (int i = 0; i < 100; i++)
        {
            st.push(i);
            printf("stack push %d\n", i);
        }
    }
    catch(std::exception& ex)
    {
        printf("excpetion: %s\n", ex.what());
    }
    try
    {
        for (int i = 0; i < 100; i++)
        {
            printf("stack pop %d\n", st.pop());
        }
    }
    catch(std::exception& ex)
    {
        printf("excpetion: %s\n", ex.what());
    }
    return 0;
}